//
//  Password_Mgr.m
//  ICE_Player
//
//  Created by  on 12-1-21.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "Password_Mgr.h"
#import "BGView.h"
#import "Globalize.h"
#include "ICE_global_var.h"

static BGView* bg_view = NULL;

@implementation Password_Mgr

- (void)On_Push_Lock_Btn
{
    if (!bg_view)
    {
        bg_view = [BGView get_Last_BG_view];
    }
    bg_view.psw_box.delegate = self;
    [bg_view On_Hide_All_Controls];
    if (gf_get_ice_cfg()->is_files_locked)
    {
        is_locking_file = NO;
        [bg_view On_Call_Passwords_View:NO];
    }
    else
    {
        is_locking_file = YES;
        memset(dest_psw, 0, sizeof(dest_psw));
        if (strlen(gf_get_ice_cfg()->pass_word) >= 6)
        {
            strcpy(dest_psw, gf_get_ice_cfg()->pass_word);
            [bg_view On_Call_Change_Psw_MB];
        }
        else
        {
            [bg_view On_Call_Passwords_View:YES];
        }
    }
    bg_view.lock_files_btn.userInteractionEnabled = NO;
}

- (BOOL)Lock_files
{
    strcpy(gf_get_ice_cfg()->pass_word, dest_psw);
    gf_get_ice_cfg()->is_files_locked = true;
    gf_set_ice_cfg(gf_get_ice_cfg());
    bg_view.lock_files_btn.state = E_LBI_LOCK;
    [bg_view On_Confirmed_Passwords];
    [bg_view.file_list_view.p_mgr start_lock_files:YES target:self call_back:@selector(On_Finished_Lock_Files) parameter:nil];
    return YES;
}

- (BOOL)Unlock_files
{
    gf_get_ice_cfg()->is_files_locked = false;
    gf_set_ice_cfg(gf_get_ice_cfg());
    bg_view.lock_files_btn.state = E_LBI_UNLOCK;
    [bg_view On_Confirmed_Passwords];
    [bg_view.file_list_view.p_mgr start_lock_files:NO target:self call_back:@selector(On_Finished_Lock_Files) parameter:nil];
    return YES;
}

- (void)On_Wrong_Passwords
{
    [bg_view On_Wrong_Passwords];
    [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(On_Wrong_Passwords_Finished) userInfo:nil repeats:NO];
}

- (void)On_Wrong_Passwords_Finished
{
    bg_view.lock_files_btn.userInteractionEnabled = YES;
    [bg_view On_leave_Passwords_View];
    [bg_view On_Display_All_Controls];
}

- (void)On_Finished_Lock_Files
{
    bg_view.lock_files_btn.userInteractionEnabled = YES;
    [bg_view On_leave_Passwords_View];
    [bg_view On_Display_All_Controls];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    if (textField.text.length == 6)
    {
        return YES;
    }
    return NO;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    char tmp[CFG_MAX_PSW_LEN] = {0};
    [textField.text getCString:tmp maxLength:CFG_MAX_PSW_LEN encoding:NSUTF8StringEncoding];
    if (is_locking_file)
    {
        strcpy(dest_psw, tmp);
        [bg_view On_Call_Psw_Confirm_MB:textField.text];
    }
    else
    {
        if (strcmp(tmp, gf_get_ice_cfg()->pass_word) != 0)
        {
            [self On_Wrong_Passwords];
        }
        else
        {
            [self Unlock_files];
        }
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    BOOL ret_val = YES;
    NSMutableString* new_s = [NSMutableString stringWithString:textField.text];
    [new_s replaceCharactersInRange:range withString:string];
    if (new_s.length > 6)
    {
        ret_val = NO;
    }
    return ret_val;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.text.length == 6)
    {
        [textField endEditing:NO];
        return YES;
    }
    return NO;
}

@end
